package com.cyj.minio.config;

import cn.hutool.core.date.DateUtil;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Swagger2配置文件
 * @BelongsProject: SpringBoot-MinIO
 * @BelongsPackage: com.cyj.minio.config
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-03 14:09
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {

    /**
     * @return
     * @Description: 创建 管理后台Admin restApi
     * @Author ChenYongJia
     * @Date 2020-12-8 11:24:19
     */
    @Bean
    public Docket createRestApi() {
        log.info("swagger2 创建 springboot+minio 8.0.3 案例项目 restApi 配置信息初始化{}", DateUtil.now());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Minio文件管理")
                .apiInfo(apiInfo())
                .select()
                // 匹配 com.jmcc.control.future.app 下所有文件
                .apis(RequestHandlerSelectors.basePackage("com.cyj.minio"))
                // 添加路径过滤，设置为全部都符合
                .paths(PathSelectors.any())
                .build();
                // 可以添加多个header或参数
                //.globalOperationParameters(getParameters());
        // 设置 Header 必须传递的参数即 token
        //.securitySchemes(securitySchemes())
    }

    /**
     * @return
     * @Description: 构建UI
     * @Author ChenYongJia
     * @Date 2021-9-3 14:14:25
     */
    @Bean
    public UiConfiguration uiConfiguration() {
        return UiConfigurationBuilder.builder()
                .supportedSubmitMethods(new String[]{})
                .build();
    }

    /**
     * @return
     * @Description: 设置 Header 必须传递的参数即 token，暂时不需要
     * @Author ChenYongJia
     * @Date 2020-12-8 11:24:19
     */
    private List<ApiKey> securitySchemes() {
        log.info("swagger2 设置 Header 必须传递的参数即 token");
        ArrayList<ApiKey> list = new ArrayList<>();
        // 设置Header必传参数
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        list.add(apiKey);
        return list;
    }

    /**
     * @return
     * @Description: 全局参数。 接口请求header中的token。 required false非必须
     * @Author ChenYongJia
     * @Date 2020-12-8 11:24:19
     */
    private List<Parameter> getParameters() {
        log.info("swagger2 全局参数。 接口请求header中的token。 required false非必须");
        List<Parameter> pars = new ArrayList<>();
        // 携带 Content-Type 不必填
        pars.add(new ParameterBuilder()
                .name("Content-Type")
                .description("Content-Type")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .defaultValue("application/json")
                .build());
        // 需要携带 Authorization 切必填
        pars.add(new ParameterBuilder()
                .name("Authorization")
                .description("Authorization")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .defaultValue("")
                .build());
        return pars;
    }

    /**
     * @return
     * @Description: 构建 api文档的信息
     * @Author ChenYongJia
     * @Date 2020-12-8 11:24:19
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("SpringBoot整合MinIODemo案例 V1.0 API接口文档")
                // 设置联系人
                .contact(new Contact("陈永佳", "https://juejin.cn/user/862483929905639", "chen867647213@163.com"))
                // 描述
                .description("SpringBoot整合MinIODemo案例 V1.0 接口文档")
                // 定义版本号
                .version("1.0").build();
    }


}
